/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.me.counttimer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
import org.me.counttimer.R;

/**
 * This class has been pulled from the Android platform source code, its an internal widget that hasn't been
 * made public so its included in the project in this fashion for use with the preferences screen; I have made
 * a few slight modifications to the code here, I simply put a MAX and MIN default in the code but these values
 * can still be set publically by calling code.
 *
 * @author Google
 */
public class CircleTimer extends View  {

  private static final String TAG = "NumberPicker";
  private List<ObjTest> arcArray = new ArrayList<ObjTest>() ;
  private int total_second = 0 ;
  private int second = 0 ;

  public CircleTimer(Context context) {
    this(context, null);
  }

  public CircleTimer(Context context, AttributeSet attrs) {
      this(context, attrs, 0);
  }

  @SuppressWarnings({"UnusedDeclaration"})
  public CircleTimer(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs);
      //setOrientation(VERTICAL);
      //LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      //inflater.inflate(new Panel(context) , this , true);
  }

  public void addArc(int second , int color){
    ObjTest obj = new ObjTest() ;

    obj.color = color ;
    obj.second = second ;
    this.total_second += obj.second ;

    this.arcArray.add(obj) ;
  }

  public void setHand(int _second){
    this.second = _second % this.total_second ;
    //this.refreshDrawableState();
    this.invalidate();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      setMeasuredDimension(220,220);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    RectF arcRectBG = new RectF(100/2-20 , 100/2-20 , 220 , 220) ;

    Paint p = new Paint() ;
    p.setColor(0xff000000) ;

    canvas.drawArc(arcRectBG , 0, 360 , true , p);

    RectF arcRect = new RectF(100/2 , 100/2 , 200 , 200) ;

    Paint a = new Paint() ;

    ObjTest Obj ;
    int len = this.arcArray.size() ;
    int start_angle = 0 ;
    int angle = 0 ;
    
    for(int i = 0 ; i < len ; ++i){
      Obj = arcArray.get(i) ;
      angle = (Obj.second * 360) / this.total_second ;

      //a.setARGB(255, Obj.red, Obj.green, Obj.blue) ;
      a.setAlpha(255) ;
      a.setColor(Obj.color) ;
      canvas.drawArc(arcRect , start_angle-90, angle , true , a);
      start_angle += angle-1 ;
      
    }

    RectF arcRectHand = new RectF(100/2-20 , 100/2-20 , 220 , 220) ;

    Paint pHand = new Paint() ;
    p.setColor(0xffFFFFFF) ;

    if(this.total_second > 0){
      Log.i("INFOI" , this.second+" > "+Integer.toString(((this.second * 360)/this.total_second)));
      canvas.drawArc(arcRectBG , ((this.second * 360)/this.total_second)-90, 5 , true , p);
    }

    //canvas.drawColor(Color.BLACK);
    //canvas.drawBitmap(_scratch, 10, 10, null);
  }

  private class ObjTest {
    public int color = 0 ;
    public int second = 0 ;
  }
}